<?php

namespace App\Model\Common;

use Illuminate\Support\Facades\DB;
use App\Model\Common\ApiErrorCode as Error;
use App\Model\Common\ApiInputParam as Input;
use App\Model\Common\ApiOutputParam as Output;
use App\Model\Common\ProjectCategory as Category;

class CategoryApi extends Common
{
    protected $cacheKey = 'ap:api:list';

    public function add($data, $opt = [])
    {
        $Api = $this;
        DB::transaction(function () use ($data, $Api) {
            $data['user_id'] = session('user:uid');

            $Api->fillData($data)->save();

            $input = [];
            $len = !empty($data['input_param']) ? count($data['input_param']) : 0;
            for ($i = 0; $i < $len; ++$i) {
                $input[$i]['param'] = $data['input_param'][$i];
                $input[$i]['type'] = $data['input_type'][$i];
                $input[$i]['limited'] = $data['input_limited'][$i];
                $input[$i]['required'] = $data['input_required'][$i];
                $input[$i]['description'] = $data['input_description'][$i];
                $input[$i]['api_id'] = $Api->id;
            }
            if ($input){
                $Input = new Input();
                $Input->insert($input);
            }

            $output = [];
            $len = !empty($data['output_param']) ? count($data['output_param']) : 0;
            for ($i = 0; $i < $len; ++$i) {
                $output[$i]['param'] = $data['output_param'][$i];
                $output[$i]['type'] = $data['output_type'][$i];
                $output[$i]['description'] = $data['output_description'][$i];
                $output[$i]['api_id'] = $Api->id;
            }
            if ($output){
                $Output = new Output();
                $Output->insert($output);
            }

            $error = [];
            $len = !empty($data['error_code']) ? count($data['error_code']) : 0;
            for ($i = 0; $i < $len; ++$i) {
                $error[$i]['code'] = $data['error_code'][$i];
                $error[$i]['description'] = $data['error_description'][$i];
                $error[$i]['api_id'] = $Api->id;
            }
            if ($error)
            {
                $Error = new Error();
                $Error->insert($error);
            }
        });

        DB::commit();

        return $Api->id;
    }

    public function edit($data, $opt = [])
    {
        $request = request();
        $uid = session('user:uid');
        DB::beginTransaction();
        $Input = new Input();
        $Output = new Output();
        $Error = new Error();
        try {
            $this->where('id', $data['id'])->where('user_id', $uid)->update($request->only($this->getColumns()));

            $len_input = !empty($data['input_param']) ? count($data['input_param']) : 0;
            $len_input_id = !empty($data['input_param_id']) ? count($data['input_param_id']) : 0;
            $old_input = [];
            for ($i = 0; $i < $len_input_id; ++$i) {
                $old_input[$i]['id'] = $data['input_param_id'][$i];
                $old_input[$i]['param'] = $data['input_param'][$i];
                $old_input[$i]['type'] = $data['input_type'][$i];
                $old_input[$i]['limited'] = $data['input_limited'][$i];
                $old_input[$i]['required'] = $data['input_required'][$i];
                $old_input[$i]['description'] = $data['input_description'][$i];
            }
            foreach ($old_input as $input) {
                $Input->where('id', $input['id'])->where('api_id', $data['id'])->update($input);
            }

            if ($len_input - $len_input_id > 0) {
                $new_input = [];
                for ($i = $len_input_id; $i < $len_input; ++$i) {
                    $new_input[$i]['param'] = $data['input_param'][$i];
                    $new_input[$i]['type'] = $data['input_type'][$i];
                    $new_input[$i]['limited'] = $data['input_limited'][$i];
                    $new_input[$i]['required'] = $data['input_required'][$i];
                    $new_input[$i]['description'] = $data['input_description'][$i];
                    $new_input[$i]['api_id'] = $data['id'];
                }
                $Input->insert($new_input);
            }

            $len_output = !empty($data['output_param']) ? count($data['output_param']) : 0;
            $len_output_id = !empty($data['output_param_id']) ? count($data['output_param_id']) : 0;
            $old_output = [];
            for ($i = 0; $i < $len_output_id; ++$i) {
                $old_output[$i]['id'] = $data['output_param_id'][$i];
                $old_output[$i]['param'] = $data['output_param'][$i];
                $old_output[$i]['type'] = $data['output_type'][$i];
                $old_output[$i]['description'] = $data['output_description'][$i];
            }
            foreach ($old_output as $output) {
                $Output->where('id', $output['id'])->where('api_id', $data['id'])->update($output);
            }

            if ($len_output - $len_output_id > 0) {
                $new_output = [];
                for ($i = $len_output_id; $i < $len_output; ++$i) {
                    $new_output[$i]['param'] = $data['output_param'][$i];
                    $new_output[$i]['type'] = $data['output_type'][$i];
                    $new_output[$i]['description'] = $data['output_description'][$i];
                    $new_output[$i]['api_id'] = $data['id'];
                }
                $Output->insert($new_output);
            }

            $len_error = !empty($data['error_code']) ? count($data['error_code']) : 0;
            $len_error_id = !empty($data['error_code_id']) ? count($data['error_code_id']) : 0;
            $old_error = [];
            for ($i = 0; $i < $len_error_id; ++$i) {
                $old_error[$i]['id'] = $data['error_code_id'][$i];
                $old_error[$i]['code'] = $data['error_code'][$i];
                $old_error[$i]['description'] = $data['error_description'][$i];
            }
            foreach ($old_error as $error) {
                $Error->where('id', $error['id'])->where('api_id', $data['id'])->update($error);
            }

            if ($len_error - $len_error_id > 0) {
                $new_error = [];
                for ($i = $len_error_id; $i < $len_error; ++$i) {
                    $new_error[$i]['code'] = $data['error_code'][$i];
                    $new_error[$i]['description'] = $data['error_description'][$i];
                    $new_error[$i]['api_id'] = $data['id'];
                }
                $Error->insert($new_error);
            }

            DB::commit();
        } catch (\Illuminate\Database\QueryException $e) {
            DB::rollBack();
            $this->error = $e->errorInfo[2];

            return false;
        }

        return true;
    }

    public function oneDataFormat($data, $opt = [])
    {
        if (!empty($opt['module'])) {
            if ('console' === $opt['module']) {
                $categoryid = $data->category_id;
                $Category = new Category();
                $category = $Category->getOne($categoryid, $opt);

                $projectid = $category['project_id'];
                $Project = new Project();
                $project = $Project->getOne($projectid, $opt);

                $data['category'] = $category;
                $data['project'] = $project;
            }

            $Input = new Input();
            $Output = new Output();
            $Error = new Error();
            $inputs = $Input->where('status', 1)->where('api_id', $data['id'])->orderBy('rank', 'desc')->orderBy('id', 'asc')->get();
            $outputs = $Output->where('status', 1)->where('api_id', $data['id'])->orderBy('rank', 'desc')->orderBy('id', 'asc')->get();
            $errors = $Error->where('status', 1)->where('api_id', $data['id'])->orderBy('rank', 'desc')->orderBy('id', 'asc')->get();
            $data['inputs'] = $inputs;
            $data['outputs'] = $outputs;
            $data['errors'] = $errors;
        }

        return $data;
    }

    public function listDataFormat($list, $opt = [])
    {
        $Category = new Category();
        foreach ($list as $key => $value) {
            // $categoryid = is_object($value) ? $value->category_id : $value['category_id'];
            $categoryid = $value['category_id'];
            $category = $Category->getOne($categoryid);
            $list[$key]['category_title'] = $category['title'];
        }
        return $list;
    }
}
